package cn.huapu.power.energy.controller;

import cn.huapu.power.energy.bean.*;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.service.*;
import cn.huapu.power.energy.util.StringUtil;
import cn.huapu.power.energy.util.token.JwtUtil;
import cn.huapu.power.energy.vo.Ret;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import java.time.LocalDate;
import java.util.List;

/**
 * @author jzx
 * @title: AppFeesController
 * @projectName energy
 * @description: 电费单app相关接口
 * @date 2019-8-58:38
 */
@RestController
@RequestMapping("/app")
public class AppFeesController {
    private Log log = LogFactory.getLog(AppFeesController.class);

    @Autowired
    private SysUserService userService;
    @Autowired
    private PeaUserpowerfeeService feeService;
    @Autowired
    private SysClientService sysClientService;
    @Autowired
    private PeaMemberlevelService levelService;
    @Autowired
    private SysClientEngService engService;

    /**
    * @Author jzx
    * @Description //用户注册
    * @Date 9:03 2019-8-5
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Ret register(@RequestBody(required = false) PeaUserpowerfee da, HttpSession session) {
    	LocalDate now = LocalDate.now(); 
        da.setYear(now.getYear());
        da.setMonth(now.getMonthValue());
        String token = userService.registerApp(da,session);
        Ret r = new Ret();
        if(StringUtil.isNotEmpty(token)){
            r.OK("注册成功",token);
        }else{
            r.Error("注册失败",null);
        }
        return r;
    }

    /**
    * @Author jzx
    * @Description //用户信息展示
    * @Date 16:52 2019-8-5
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/userInfo",method = RequestMethod.POST)
    public Ret userInfo(HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        SysClient sysClient = sysClientService.get(user.getClientid());
        user.setClientname(sysClient.getFullname());
        user.setPassword("");
        Ret r = new Ret();
        r.OK(user);
        return r;
    }

    /**
    * @Author jzx
    * @Description //用户信息修改
    * @Date 16:17 2019-8-5
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/userEdit",method = RequestMethod.POST)
    public Ret userEdit(@RequestBody PeaUserpowerfee da, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        da.setPkid(user.getPkid());
        da.setClientid(user.getClientid());
        boolean b = userService.updateUserApp(da);
        Ret r = new Ret();
        if(b){
            r.OK("修改成功");
        }else{
            r.Error("修改失败",null);
        }
        return r;
    }

    /**
    * @Author jzx
    * @Description //用户单位信息展示
    * @Date 16:57 2019-8-5
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/clientInfo",method = RequestMethod.POST)
    public Ret clientInfo(HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        SysClientEng sysClientEng = engService.getSysClientEng(user.getClientid());
        Ret r = new Ret();
        r.OK(sysClientEng);
        return r;
    }

    /**
    * @Author jzx
    * @Description //用户单位信息修改
    * @Date 16:02 2019-8-5
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/powfeeEdit",method = RequestMethod.POST)
    public Ret powfeeEdit(@RequestBody PeaUserpowerfee da,HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("user");
        LocalDate now = LocalDate.now();       
        da.setYear(now.getYear());
        da.setMonth(now.getMonthValue());
        da.setClientid(user.getClientid());
        boolean b = feeService.powfeeEdit(da);
        Ret r = new Ret();
        if(b){
            r.OK("修改成功");
        }else{
            r.Error("修改失败",null);
        }
        return r;
    }

    /**
    * @Author jzx
    * @Description //用户等级排名接口
    * @Date 15:01 2019-8-6
    * @Param [da, session]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/userLevel",method = RequestMethod.POST)
    public Ret userLevel(@RequestBody DateParameterBo bo) {
        PeaMemberlevel peaMemberlevel = new PeaMemberlevel();
        peaMemberlevel.setClientid(bo.getId());
        List<PeaMemberlevel> peaMemberlevels = levelService.queryList(peaMemberlevel);
        Ret r = new Ret();
        r.OK(peaMemberlevels.get(0));
        return r;
    }


}
